
package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import zyy.util.DbException;
import zyy.util.Bus;

/**
 * @Author: SevDaisy十七散人
 * @Date: 2020-07-04 09:15:51
 * @Last Modified time: 2020-07-04 13:51:48
 */
public class FrmMain extends JFrame implements ActionListener {
  private static final long serialVersionUID = 1285003120300482350L;

  private JPanel welcome = new JPanel();
  private JLabel welLabel = new JLabel();
  private JPanel toolBar = new JPanel();

  // Buttons
  private JButton userGo = new JButton("点单");
  private JButton userOrders = new JButton("看单");
  private JButton userInfo = new JButton("关于我");

  private JButton riderHub = new JButton("抢单");
  private JButton riderInfo = new JButton("关于我");
  private JButton riderWork = new JButton("历史订单");

  private JButton adminUser = new JButton("用户管理");
  private JButton adminRider = new JButton("骑手管理");
  private JButton adminMerchant = new JButton("商家管理");
  private JButton adminCoupon = new JButton("优惠券管理");
  private JButton adminFull_Reduction = new JButton("满减管理");
  private JButton adminComment = new JButton("评论管理");

  // private String LoginAccountType = Link.LoginAccountType;
  // public BeanUsers currentUser = Link.currentUser;
  // public BeanRider currentRider = Link.currentRider;
  // public BeanAdministrator currentAdministrator = Link.currentAdministrator;

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.userGo) {
      try {
        Bus.currentOrder = Bus.orderManager.add(Bus.currentUser);
        System.out.println("创建新建订单 ID 是: " + Bus.currentOrder.getOrderID());
      } catch (Exception e1) {
        JOptionPane.showMessageDialog(null, "数据库新建空订单失败", "错误", JOptionPane.ERROR_MESSAGE);
        e1.printStackTrace();
      }
      FrmUserGo fUserGo = new FrmUserGo();
      fUserGo.setVisible(true);
    } else if (e.getSource() == this.userOrders) {
      System.out.println("唤起 用户界面 查看订单");
      Bus.fUserOrders = new FrmUserOrders();
    } else if (e.getSource() == this.userInfo) {
      new FrmUserInfo(Bus.currentUser.getUserID());
    } else if (e.getSource() == this.riderHub) {
      System.out.println("唤起 骑手抢单 界面");
      FrmRiderHub FRiderHub = new FrmRiderHub();
      FRiderHub.setVisible(true);
    } else if (e.getSource() == this.riderInfo) {
      // FrmRiderInfo fRiderInfo = new FrmRiderInfo();
      new FrmRiderInfo();
    } else if (e.getSource() == this.riderWork) {
      new FrmRiderWork();
    } else if (e.getSource() == this.adminUser) {
      System.out.println("唤起 管理员 用户管理");
      Bus.fAdminUser = new FrmAdminUser();
    } else if (e.getSource() == this.adminRider) {
      System.out.println("唤起 管理员 骑手管理");
      Bus.fAdminRider = new FrmAdminRider();
    } else if (e.getSource() == this.adminMerchant) {
      System.out.println("唤起 管理员 商家管理");
      Bus.fAdminMerchant = new FrmAdminMerchant();
    } else if (e.getSource() == this.adminCoupon) {
    } else if (e.getSource() == this.adminFull_Reduction) {
    } else if (e.getSource() == this.adminComment) {
    }
  }

  // public FrmMain(String type) {
  public FrmMain() {
    {
      // Main JFrame 基础设置
      // this.setExtendedState(Frame.MAXIMIZED_BOTH);
      this.setSize(440, 180);
      // this.setSize(440, 500);
      this.setTitle("外卖助手 主菜单");
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // this.setLocationRelativeTo(null);
      this.setLocation(0, 0);
      // this.setVisible(false);
      this.setVisible(true);
    }
    {
      // 唤起 登陆界面 (JDialog)
      FrmLogin dlgLogin = new FrmLogin(this, "登陆", true);
      // FrmLogin 会 设置 LoginAccountType 等相关信息
      dlgLogin.setVisible(true);
    }
    {
      // UI 加载设置
      welcome.setLayout(new FlowLayout(FlowLayout.CENTER));
      toolBar.setLayout(new FlowLayout(FlowLayout.CENTER));
      String welStr = "";
      switch (Bus.LoginAccountType) {
        case "用户":
          // System.out.println("succeed 1");
          welStr = "用户 " + Bus.currentUser.getuName() + "，你好，欢迎你的使用";
          Bus.jButtonZYYStyle(userGo);
          Bus.jButtonZYYStyle(userOrders);
          Bus.jButtonZYYStyle(userInfo);
          toolBar.add(userGo);
          toolBar.add(userOrders);
          toolBar.add(userInfo);
          break;
        case "骑手":
          welStr = "骑手 " + Bus.currentRider.getrName() + "，你好，你是" + Bus.currentRider.getrLevel();
          Bus.jButtonZYYStyle(riderHub);
          Bus.jButtonZYYStyle(riderInfo);
          Bus.jButtonZYYStyle(riderWork);
          toolBar.add(riderHub);
          toolBar.add(riderInfo);
          toolBar.add(riderWork);
          break;
        case "管理员":
          welStr = "管理员 " + Bus.currentAdministrator.getaName() + "，你好，跑路有风险，删库需谨慎！";
          Bus.jButtonZYYStyle(adminUser);
          Bus.jButtonZYYStyle(adminRider);
          Bus.jButtonZYYStyle(adminMerchant);
          Bus.jButtonZYYStyle(adminCoupon);
          Bus.jButtonZYYStyle(adminFull_Reduction);
          toolBar.add(adminUser);
          toolBar.add(adminRider);
          toolBar.add(adminMerchant);
          toolBar.add(adminCoupon);
          toolBar.add(adminFull_Reduction);
          this.adminCoupon.setFont(new java.awt.Font("STKaiti Regular", java.awt.Font.BOLD, 15));
          this.setSize(500, 155);
        default:
          break;
      }
      welLabel.setText(welStr);
      welLabel.setFont(new Font("STKaiti Regular", Font.PLAIN, 20));
      welcome.add(welLabel);
      this.getContentPane().add(welcome, BorderLayout.NORTH);
      this.getContentPane().add(toolBar, BorderLayout.CENTER);
    }
    {
      // 事件监听管理
      // 按钮事件
      this.userGo.addActionListener(this);
      this.userOrders.addActionListener(this);
      this.userInfo.addActionListener(this);
      this.riderHub.addActionListener(this);
      this.riderInfo.addActionListener(this);
      this.riderWork.addActionListener(this);
      this.adminUser.addActionListener(this);
      this.adminRider.addActionListener(this);
      this.adminMerchant.addActionListener(this);
      this.adminCoupon.addActionListener(this);
      this.adminFull_Reduction.addActionListener(this);
      this.adminComment.addActionListener(this);
      // 窗口事件
      this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          if (Bus.currentUser != null && Bus.currentOrder != null) {
            try {
              Bus.orderManager.removeByOrderID(Bus.currentOrder.getOrderID());
            } catch (DbException ex) {
              ex.printStackTrace();
              JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }
          }
          // System.exit(0);
          FrmMain.this.setVisible(false);
        }
      });
    }
    {
      this.revalidate();
    }
  }

  // public FrmMain() {
  // new FrmMain("用户");
  // FrmLogin dlg = new FrmLogin(null, "测试Login", true);
  // dlg.setVisible(true);
  // }

  public static void main(String[] args) {
    // new FrmMain("用户");
    // new FrmMain("骑手");
    // new FrmMain("管理员");
  }
}